<?php

namespace app\api\model;

use think\Db;
use app\api\model\Common;
use think\Validate;

class GroupUser extends Common
{

    public function user()
    {
        return $this->hasOne('User', 'id', 'uid');
    }

    /**
	 * [createData 新建]
	 * @linchuangbin
	 * @DateTime  2017-02-10T21:19:06+0800
	 * @param     array                    $param [description]
	 * @return    [array]                         [description]
	 */
	public function createData($param)
	{
		// 验证
        $validate = validate($this->name);
        foreach($param as $value)
        {
            if (!$validate->check($value)) {
                throw new \Exception($validate->getError());
            }
        }

		try {
			$this->allowField(true)->InsertAll($param);
			return true;
		} catch(\Exception $e) {
            throw new \Exception($e->getMessage());
		}
	}

	/**
	 * 联合列表
	 */
	public function getUnionList($id){
		$result = $this->with(['user' => function($query){$query->field('id,username');}])->where(['did'=>$id])->select();
		foreach($result as &$value)
		{
			$value = $value->toArray();
		}
		return $result;
	}

}
